# Maintainer: Jaroslav Lichtblau <svetlemodry@archlinux.org>

_realname=sdcv
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.5.5
pkgrel=1
pkgdesc="StarDict Console Version"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url="https://dushistov.github.io/sdcv/"
license=('GPL')
depends=(
  "${MINGW_PACKAGE_PREFIX}-glib2"
  "${MINGW_PACKAGE_PREFIX}-readline"
  "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-cc")
source=(${_realname}-$pkgver.tar.gz::https://github.com/Dushistov/${_realname}/archive/v$pkgver.tar.gz
        "001-sdcv-0.5.3-fix-mingw-build.patch")
sha256sums=('4d2519e8f8479b9301dc91e9cda3e1eefef19970ece0e8c05f0c7b7ade5dc94b'
            '7e629fe4a2337d7e700da4ae15b97b756a0ce2c0ccfefda58ddea308c99f2cdd')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}/001-sdcv-0.5.3-fix-mingw-build.patch"
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      ${extra_config[@]} \
      -DBUILD_TESTS=True \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
  ${MINGW_PREFIX}/bin/cmake --build . --target lang
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}

  ${MINGW_PREFIX}/bin/ctest
}

package() {
   cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
}
